import axios from 'axios'
import { Toast } from 'vant'

const service = axios.create({
    baseURL: process.env.VUE_APP_API,
    timeout: 6000
})

service.interceptors.request.use(config => {
    Toast({
        type: 'loading',
        message: '加载中',
        duration: 0
    })
    return config
}, err => {
    return Promise.reject(err)
})

service.interceptors.response.use(res => {
    Toast.clear()
    return Promise.resolve(res)
}, err => {
    if (err.response && err.response.status) {
        let msg = '出错了'
        switch (err.response.status) {
            case 400:
                msg = '请求错误'
                break
            case 401:
                msg = '未授权，请登录'
                break
            case 403:
                msg = '拒绝访问'
                break
            case 404:
                msg = `请求地址出错: ${err.response.config.url}`
                break
            case 408:
                msg = '请求超时'
                break
            case 500:
                msg = '服务器错误'
                break
            case 501:
                msg = '服务未实现'
                break
            case 502:
                msg = '网关错误'
                break
            case 503:
                msg = '服务不可用'
                break
            case 504:
                msg = '网关超时'
                break
            case 505:
                msg = 'HTTP版本不受支持'
                break
            default:
        }
        Toast({
            message: msg,
            position: 'top'
        })
    }
    return Promise.reject(err)
})

export default service